#include "CAnimation.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "CCamera.h"

CAnimation::CAnimation(void)
{
	m_pAniID = NULL;
	m_fFrameDuration = 0;
	m_ncurrentFrame = 0;
	m_nxPos = 0;
	m_nyPos = 0;

	RECT temp = {0,0,0,0};
	m_rcollisiontemp = temp;
	m_rframetemp = temp;
	m_rweapontemp = temp;

	flipped = false;

}

CAnimation::~CAnimation(void)
{
	for(unsigned int i = 0; i < m_vFrames.size(); ++i)
		delete m_vFrames[i];

	if(m_pAniID != NULL)
	{
		delete m_pAniID;
		m_pAniID = NULL;
	}

	delete[] m_szAnimationName;
	delete[] m_szTrigger;
}
CAnimation::CAnimation(const CAnimation& ani)
{
	m_nimageID = ani.m_nimageID;
	m_szAnimationName = new char[sizeof(*(ani.m_szAnimationName)+1)];
	for(unsigned int i = 0; i < sizeof(*ani.m_szAnimationName)+1; ++i)
	{
		m_szAnimationName[i] = ani.m_szAnimationName[i];
	}

	m_szTrigger = new char[sizeof(*(ani.m_szTrigger)+1)];
	for(unsigned int i = 0; i < sizeof(*ani.m_szTrigger)+1; ++i)
	{
		m_szTrigger[i] = ani.m_szTrigger[i];
	}

	m_bIsPlaying = ani.m_bIsPlaying;
	m_ncurrentFrame = ani.m_ncurrentFrame;

	m_fFrameDuration = ani.m_fFrameDuration;
	m_nNumFrames = ani.m_nNumFrames;
	
	for(unsigned int i = 0; i < ani.m_vFrames.size(); ++i)
	{
		m_vFrames.push_back(new CFrames());
		*m_vFrames[i] = *(ani.m_vFrames[i]);
	}

	flipped = false;
}

CFrames* CAnimation::GetFrame() 
{ 
	return m_vFrames[m_ncurrentFrame];
}

void CAnimation::AddFrame(CFrames* frame)
{
	m_vFrames.push_back(frame);
}

void CAnimation::Update(float dt)
{

	if(m_bIsPlaying)
	{
		m_fFrameDuration += dt;

		
		if(m_fFrameDuration >= m_vFrames[m_ncurrentFrame]->GetFrameDuration())
		{
			m_fFrameDuration = 0.0f;
			m_ncurrentFrame++;
			if(m_ncurrentFrame >= m_nNumFrames)
				if(m_blooping)
					m_ncurrentFrame = 0;
				else
				{
					m_bIsPlaying = false;
					m_ncurrentFrame = 0;
				}
		}
	}
	else
		m_ncurrentFrame = 0;
}
void CAnimation::Render()
{
	if(m_bIsPlaying)
	{
	CSGD_Direct3D::GetInstance()->SpriteEnd();

	m_rframetemp.left   = m_nxPos - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX();
	m_rframetemp.right  = ((m_vFrames[m_ncurrentFrame]->GetFrameRect().right - m_vFrames[m_ncurrentFrame]->GetFrameRect().left) + m_nxPos) - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX();
	m_rframetemp.top    = m_nyPos - m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY();
	m_rframetemp.bottom = ((m_vFrames[m_ncurrentFrame]->GetFrameRect().bottom - m_vFrames[m_ncurrentFrame]->GetFrameRect().top) + m_nyPos) -  m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY();

	m_rcollisiontemp.left   = m_nxPos + m_vFrames[m_ncurrentFrame]->GetCollisionRect().left - m_vFrames[m_ncurrentFrame]->GetFrameRect().left - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX();
	m_rcollisiontemp.right  = m_nxPos + m_vFrames[m_ncurrentFrame]->GetCollisionRect().right - m_vFrames[m_ncurrentFrame]->GetFrameRect().left - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX();
	m_rcollisiontemp.top    = m_nyPos + m_vFrames[m_ncurrentFrame]->GetCollisionRect().top - m_vFrames[m_ncurrentFrame]->GetFrameRect().top - m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY();
	m_rcollisiontemp.bottom = m_nyPos + m_vFrames[m_ncurrentFrame]->GetCollisionRect().bottom - m_vFrames[m_ncurrentFrame]->GetFrameRect().top - m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY();

	m_rweapontemp.left   = m_nxPos + m_vFrames[m_ncurrentFrame]->GetWeaponRect().left - m_vFrames[m_ncurrentFrame]->GetFrameRect().left - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX();
	m_rweapontemp.right  = m_nxPos + m_vFrames[m_ncurrentFrame]->GetWeaponRect().right - m_vFrames[m_ncurrentFrame]->GetFrameRect().left - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX();
	m_rweapontemp.top	 = m_nyPos + m_vFrames[m_ncurrentFrame]->GetWeaponRect().top - m_vFrames[m_ncurrentFrame]->GetFrameRect().top - m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY();
	m_rweapontemp.bottom = m_nyPos + m_vFrames[m_ncurrentFrame]->GetWeaponRect().bottom - m_vFrames[m_ncurrentFrame]->GetFrameRect().top - m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY();
	
	if(flipped)
	{
		m_rweapontemp.right	-= (m_vFrames[m_ncurrentFrame]->GetWeaponRect().right - m_vFrames[m_ncurrentFrame]->GetWeaponRect().left) + 
			((m_vFrames[m_ncurrentFrame]->GetWeaponRect().left - m_vFrames[m_ncurrentFrame]->GetFrameRect().left) - m_vFrames[m_ncurrentFrame]->GetAnchorX());

		m_rweapontemp.left	-= (m_vFrames[m_ncurrentFrame]->GetWeaponRect().right - m_vFrames[m_ncurrentFrame]->GetWeaponRect().left) + 
			((m_vFrames[m_ncurrentFrame]->GetWeaponRect().left - m_vFrames[m_ncurrentFrame]->GetFrameRect().left) - m_vFrames[m_ncurrentFrame]->GetAnchorX());
	}
	//CSGD_Direct3D::GetInstance()->DrawRect(m_rframetemp, 0,0,0);
	//CSGD_Direct3D::GetInstance()->DrawRect(m_rcollisiontemp, 0, 255, 0);
	CSGD_Direct3D::GetInstance()->DrawRect(m_rweapontemp, 255, 0, 0);
	CSGD_Direct3D::GetInstance()->SpriteBegin();

	if(!flipped)
	{
		CSGD_TextureManager::GetInstance()->Draw(GetID(), m_nxPos - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX(), 
			m_nyPos - m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY(), 1.0f, 1.0f, &m_vFrames[m_ncurrentFrame]->GetFrameRect(), 
			(float)m_vFrames[m_ncurrentFrame]->GetFrameRect().right/2, (float)m_vFrames[m_ncurrentFrame]->GetFrameRect().bottom/2);
	}

	else
	{
		CSGD_TextureManager::GetInstance()->Draw(GetID(), m_nxPos - m_vFrames[m_ncurrentFrame]->GetAnchorX() - CCamera::GetInstance()->GetCameraPosX() + (m_vFrames[m_ncurrentFrame]->GetFrameRect().right - m_vFrames[m_ncurrentFrame]->GetFrameRect().left), 
			m_nyPos - m_vFrames[m_ncurrentFrame]->GetAnchorY() - CCamera::GetInstance()->GetCameraPosY(), -1.0f, 1.0f, &m_vFrames[m_ncurrentFrame]->GetFrameRect(), 
			(float)m_vFrames[m_ncurrentFrame]->GetFrameRect().right/2, (float)m_vFrames[m_ncurrentFrame]->GetFrameRect().bottom/2);
	}
	}
}
